package com.zhangxu.microservice.system.dao.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangxu.microservice.system.dao.condition.ResourceCondition;
import com.zhangxu.microservice.system.dao.entity.ResourceDO;
import com.zhangxu.microservice.system.dao.mapper.ResourceMapper;
import com.zhangxu.microservice.system.dao.service.ResourceDao;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * 资源DAO实现类
 */
@Repository
public class ResourceDaoImpl extends ServiceImpl<ResourceMapper, ResourceDO> implements ResourceDao {


    @Override
    public List<ResourceDO> getByAppId(Long appId) {
        return this.lambdaQuery()
                .eq(Objects.nonNull(appId), ResourceDO::getAppId, appId)
                .eq(ResourceDO::getDeleteTime, 0L)
                .list();
    }

    @Override
    public List<ResourceDO> listByAppIdAndResourceCodes(Long appId, List<String> resourceCodes) {
        return this.lambdaQuery()
                .eq(ResourceDO::getAppId, appId)
                .in(ResourceDO::getCode, resourceCodes)
                .eq(ResourceDO::getDeleteTime, 0L)
                .list();
    }


    @Override
    public ResourceDO getById(Serializable id) {
        return this.lambdaQuery()
                .eq(ResourceDO::getId, id)
                .eq(ResourceDO::getDeleteTime, 0L)
                .one();
    }

    @Override
    public IPage<ResourceDO> page(ResourceCondition condition) {
        condition.checkPage();
        return this.lambdaQuery()
                .eq(condition.getAppId() != null, ResourceDO::getAppId, condition.getAppId())
                .eq(StrUtil.isNotBlank(condition.getParentCode()), ResourceDO::getParentCode, condition.getParentCode())
                .like(StrUtil.isNotBlank(condition.getName()), ResourceDO::getName, condition.getName())
                .eq(StrUtil.isNotBlank(condition.getCode()), ResourceDO::getComponent, condition.getCode())
                .eq(condition.getType() != null, ResourceDO::getType, condition.getType())
                .isNull(ResourceDO::getDeleteTime)
                .orderByAsc(ResourceDO::getId)
                .page(condition.getIpage());
    }
}